#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include"xh.h"
struct SqQueue {
	ElementType* arr;
	int front;
	int rear;
	int capicity;
};

queue creatqueue() {
	queue Q =(struct SqQueue*) malloc(sizeof(struct SqQueue));
	if (Q == NULL) {
		perror("malloc");
		return NULL;
	}

	Q->arr =(ElementType*) malloc(max_size*sizeof(ElementType));
	if (Q->arr == NULL) {
		perror("malloc");
		return NULL;
	}
	Q->capicity = max_size;
	Q->front = Q->rear = 0;
	return Q;
}

int queueLength(queue Q) {
	return ((Q->rear - Q->front + max_size) % max_size);
}

void enqueue(queue Q,ElementType x) {
	if ((Q->rear + 1) % max_size == Q->front) {
		printf("queue is full\n");
	}
	Q->arr[Q->rear] = x;
	Q->rear = (Q->rear + 1) % max_size;
}

ElementType Dequeue(queue Q) {
	if (Q->front == Q->rear) {
		printf("Empty");
	}
	ElementType x;
	x = Q->arr[Q->front];
	Q->front = (Q->front + 1) % max_size;
	return x;
}

ElementType gethead(queue Q) {
	if (Q->rear != Q->front)
       	return Q->arr[Q->front];
	return ERROR;
}

void showqueue(queue Q){
	int i = Q->front;
	while (i!=Q->rear) {
		printf("%d ", Q->arr[i]);
		i++;
	}
}
